/*
 * @Author: your name
 * @Date: 2021-05-26 15:59:44
 * @LastEditTime: 2021-05-28 15:10:51
 * @LastEditors: Please set LastEditors
 * @Description: In User Settings Edit
 * @FilePath: \bazai_music_api\app.js
 */
const Koa = require('koa')
const app = new Koa()
const Router = require('koa-router')
const router=  new Router()
const cors = require('koa2-cors')
const ENV = 'cloud1-9go21hrud33d2148'
const koaBody = require('koa-body')
const compose = require('koa-compose')
const compress = require('koa-compress');


const playlsit = require('./controller/playlist.js')
const swiper = require('./controller/swiper.js')
const blog = require('./controller/blog.js')
router.use('/playlist', playlsit.routes())
router.use('/swiper', swiper.routes())
router.use('/blog', blog.routes())

// 使用koa-compose 集成中间件
const middleware = compose([
	// 接收post参数解析
	koaBody({
		multipart: true, // 是否允许接收formdata数据
		encoding: 'utf-8', // 编码格式
		keepExtensions: true, // 保持文件的后缀
		jsonLimit: '5mb',
		textLimit: '5mb',
		formidable: { // 设置文件大小
		  keepExtensions: true, // 扩展大小
		  maxFieldsSize: 5 * 1024 * 1024 // 大小设置为 5MB
		},
		onError: err => {
		  console.log('koabody TCL: err', err);
		}
	}),
	// 跨域
	cors({
		origin: ['http://localhost:9528'],
		credentials: true
	})
])


app.use(async (ctx, next) => {
	console.log('全局中间件')
	ctx.state.env = ENV
	await next()
})

app.use(compress())
app.use(middleware)
app.use(router.routes())
app.use(router.allowedMethods())


app.listen(3000, () => {
    console.log('服务开启在3000端口')
})

// MVC模式